package cn.hackcloud.concurrency.readwritelock;

/**
 * 测试类
 * Created by www.ibfbc.com
 */
public class ReadWriteLockTest {
    private final static String text = "thisisreadwritlocktest";

    public static void main(String[] args) {
        final ShareData shareData = new ShareData(50);
        //创建2个线程进行写操作
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                for (int index = 0; index < text.length(); index++) {
                    try {
                        char c = text.charAt(index);
                        shareData.write(c);
                        System.out.println(Thread.currentThread().getName() + " write:" + c);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }
        //创建10个线程读取数据
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                while (true) {
                    try {
                        System.out.println(Thread.currentThread().getName() + " read:" + new String(shareData.read()));
                    } catch (Exception e) {
                    }
                }
            }).start();

        }
    }
}
